<template>
  <div>
    <el-form ref="pram" :model="pram" :rules="rules" label-width="100px" @submit.native.prevent>
      <el-form-item label="回访人" prop="visitPerson">
        <el-input v-model="pram.visitPerson" maxlength="50" placeholder="回访人" />
      </el-form-item>

      <el-form-item label="回访人电话" prop="visitTel">
        <el-input v-model="pram.visitTel" maxlength="50" placeholder="回访人电话" />
      </el-form-item>

      <el-form-item label="回访时间" prop="visitTime">
        <el-input v-model="pram.visitTime" maxlength="50" placeholder="回访时间" />
      </el-form-item>

      <el-form-item label="回访记录" prop="notes">
        <el-input v-model="pram.notes" maxlength="50" placeholder="回访记录" />
      </el-form-item>

      <el-form-item>
        <el-button type="primary" @click="handlerSubmit('pram')" v-hasPermi="['admin:notice:update','admin:notice:save']">{{ isCreate===0?'确定':'更新' }}</el-button>
        <el-button @click="close">取消</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import Tinymce from '@/components/Tinymce/index'
import * as visitRecord from '@/api/visitRecord.js'
import  * as noticeApi from '@/api/noticeApi.js'
import * as roleApi from '@/api/role.js'
import {Debounce} from '@/utils/validate'
import bus from '@/api/bus.js'
import { updateVisit } from '@/api/visitRecord.js'

export default {
  // name: "edit"
  components: { Tinymce },
  // props: {
  //   isCreate: {
  //     type: Number,
  //     required: true
  //   },
  //   editData: {
  //     type: Object,
  //     default: () => {
  //       return { rules: [] }
  //     }
  //   },
  // },
  data() {
    return {
      constants:this.$constants,
      pram: {
        id:0,
        visitPerson: "",
        visitTel: "",
        visitTime: "",
        notes: 0,
        complaintId:null
      },
      rules: {
        visitPerson: [{ required: true, message: '请填写回访人姓名', trigger: ['blur', 'change'] }],
        visitTel: [{ required: true, message: '请填写回访人电话', trigger: ['blur', 'change'] }],
        // idCardFront: [{ required: true, message: '请上传身份证正面', trigger: ['blur', 'change'] }],
        // idCardBack: [{ required: true, message: '请上传身份证反面', trigger: ['blur'] }],
        visitTime: [{ required: true, message: '请填写回访时间', trigger: ['blur'] }],
      },
    }
  },
  props:['id','complaintId1','isCreate','editData'],
  mounted() {
    console.log("editData:"+this.editData)
    // this.pram.visitPerson = this.editData.visitPerson;
    // this.pram.visitTel = this.editData.visitTel;
    // this.pram.visitTime = this.editData.visitTime;
    // this.pram.notes = this.editData.notes;
    this.pram.complaintId = this.complaintId1
    console.log(this.pram.complaintId)

    this.initEditData()

  },
  created(){
    // 定义事件,注意箭头函数应用

  },
  methods: {
    // hanleSendBus(){
    //   bus.$on('sendMsg',msg=>{
    //     this.pram.complaintId = msg
    //     console.log(89)
    //     console.log(msg)
    //   })
    // },
    close() {
      this.$emit('hideEditDialog')
    },
    initEditData() {
      if (this.isCreate !== 1) return //编辑
      const { visitPerson, visitTel, visitTime, notes,id } = this.editData //通过list中slot-scope=“scope“，scope.$index就是该行的下标，scope.row就是该行的数据所有消息对象，有了这两个参数就可以实现编辑、删除功能（this.editData就是scope.row传入的对象参数）
      this.pram.visitPerson = visitPerson
      this.pram.visitTel = visitTel
      this.pram.visitTime = visitTime
      this.pram.notes = notes
      this.pram.id = id
    },

    handlerSubmit:Debounce(function(form) {

      this.$refs[form].validate(valid => {
        if (!valid) return
        if (this.isCreate === 0) {
          this.handlerSave()
        } else {
          this.handlerEdit()
        }
      })
    }),
    // getVisitRecordsList(){
    //   visitRecord.getVisitRecordsList(this.listPram).then(data => {
    //     this.pram = data
    //   })
    // },
    handlerSave() {
      console.log(1111)
      console.log(this.pram)
      visitRecord.insertVisit(this.pram).then(data => {
        this.$message.success('添加回访记录成功')
        this.$emit('hideEditDialog')
      })
    },
    handlerEdit() {
      visitRecord.insertVisit(this.pram,this.id,this.rules).then(data => {
        this.$message.success('更新回访记录成功')
        this.$emit('hideEditDialog')
      })
    },
  }
}
</script>

<style scoped>

</style>
